<?php
namespace app\index\controller;


use app\index\model\DeptModel;
use app\index\model\EnginChanListModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginJianModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginSpeedModel;
use app\index\model\EnginStampListModel;
use app\index\model\EnginStampModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengListModel;
use app\index\model\EnginZhengModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\SpeedMonthListModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\UserModel;
use app\index\model\VisaModel;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\SceneService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Scene extends Common
{

    public function list_engin_engin_diary(Request $request){
        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $data = EnginModel::alias('e')
            ->field('e.id,e.name as title')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_diary', 'e'))
            ->order('e.id DESC')->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_diary(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['diary.deleted_user','=', 0];
        $where[] = ['diary.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['diary.enginid','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['diary.tianer','like', '%'.$param['keyword'].'%'];
        }
//        if(isset($param['enginname']) && !empty($param['enginname'])){
//            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
//        }
//        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
//            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
//        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['diary.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['tian']) && !empty($param['tian'])){
            $where[] = ['diary.tian','like', '%'.$param['tian'].'%'];
        }
        if(isset($param['wen']) && !empty($param['wen'])){
            $where[] = ['diary.wen','like', '%'.$param['wen'].'%'];
        }
        if(isset($param['feng']) && !empty($param['feng'])){
            $where[] = ['diary.feng','like', '%'.$param['feng'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['diary.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['jier']) && !empty($param['jier'])){
            $where[] = ['diary.jier','like', '%'.$param['jier'].'%'];
        }
        if(isset($param['tianer']) && !empty($param['tianer'])){
            $where[] = ['diary.tianer','like', '%'.$param['tianer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['diary.status','=', $param['status']];
        }

        $data['data'] = EnginDiaryModel::alias('diary')
            ->field('diary.*,e.name as enginname')
            ->join('engin e','e.id=diary.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_diary', 'e'))
            ->order('diary.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_diary');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择日期');
            }
            if(empty($param['tian'])){
                return outputJson(4000, '请输入天气情况');
            }

            if((new SceneService())->check_engin_diary_dt($param['dt'], $param['enginid'])){
                return outputJson(4000, '该项目日志日期已存在');
            }



            $param['tianer'] = $this->userinfo['realname'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginDiaryModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_diary',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_diary', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_diary', $id, '施工日志');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['tianer'] = $this->userinfo['realname'];
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginDiaryModel::where(['id'=>$param['id']])->find();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择日期');
            }
            if(empty($param['tian'])){
                return outputJson(4000, '请输入天气情况');
            }

            if((new SceneService())->check_engin_diary_dt($param['dt'], $old['enginid'], $param['id'])){
                return outputJson(4000, '该项目日志日期已存在');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginDiaryModel::update($param);
                (new OptService())->relation_file($files,'engin_diary',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_diary', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_diary', $param['id'], '施工日志');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginDiaryModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['files'] = (new OptService())->get_file('engin_diary', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_diary(Request $request){
        $param = $request->post();
        $data = EnginDiaryModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginDiaryModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_diary', $data['id'], '施工日志');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_diary(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginDiaryModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $data['files'] = (new OptService())->get_file('engin_diary', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_speed_zong(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zong.deleted_user','=', 0];
        $where[] = ['zong.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|zong.number|zong.name|zong.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zong.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['zong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['zong.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['zong.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['zong.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['zong.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['zong.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedZongModel::alias('zong')
            ->field('zong.*,e.name as enginname')
            ->join('engin e','e.id=zong.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_zong', 'zong'))
            ->order('zong.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wanchan'] = 0;
                $item['wanbi'] = 0;
                $item['findt'] = '';
                $item['jinbi'] = '';

                $zongList = SpeedZongListModel::where(['zongid'=>$item['id']])->select()->toArray();
                foreach ($zongList as $v){
                    $findt = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->value('list.findt');
                    if(!empty($findt)){
                        if(empty($item['findt'])){
                            $item['findt'] = $findt;
                        }else{
                            if($findt <= $item['findt']){
                                $item['findt'] = $findt;
                            }
                        }
                    }
                    $shinum = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->sum('list.shinum');
                    $item['wanchan'] += $shinum*$v['price'];
                }

                if($item['chanfee'] != 0){
                    $item['wanbi'] = round($item['wanchan'] / $item['chanfee'] *100, 2);
                }

                if(!empty($item['findt'])){
                    $jihua = get_time_slot_days($item['startdt'], $item['enddt']);
                    $shiji = get_time_slot_days($item['findt'], date("Y-m-d"));
                    if($jihua != 0){
                        $item['jinbi'] = round($shiji/$jihua*100, 2);
                    }
                }
                if(!$item['jinbi']){
                    $item['jinbi'] = 0;
                }


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('speed_zong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_speed_zong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入进度计划名称');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择计划开始日期');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择计划结束日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            $exist = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();
            if($exist){
                return outputJson(4000, '该项目总进度计划已存在');
            }


            $param['number'] = (new SceneService())->create_speed_zong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }
            Db::startTrans();
            try {
                $id = SpeedZongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zongid'] = $id;
                    SpeedZongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_zong',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'speed_zong', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'speed_zong', $id, '总进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SceneService())->create_speed_zong_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_speed_zong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SpeedZongModel::where(['id'=>$param['id']])->find();


            if(empty($param['name'])){
                return outputJson(4000, '请输入进度计划名称');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择计划开始日期');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择计划结束日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            $exist = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$old['enginid']])
                ->where('id','neq',$old['id'])->find();
            if($exist){
                return outputJson(4000, '该项目总进度计划已存在');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }


            Db::startTrans();
            try {
                SpeedZongModel::update($param);

                SpeedZongListModel::where(['zongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zongid'] = $param['id'];
                    SpeedZongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_zong',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'speed_zong', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'speed_zong', $param['id'], '总进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SpeedZongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = SpeedZongListModel::where(['zongid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('speed_zong', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_speed_zong(Request $request){
        $param = $request->post();
        $data = SpeedZongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            SpeedZongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'speed_zong', $data['id'], '总进度计划');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_speed_zong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SpeedZongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = SpeedZongListModel::where(['zongid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('speed_zong', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_speed_month(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|month.number|month.name|month.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['month.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['month.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['month.month','=', $param['month']];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['month.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['month.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedMonthModel::alias('month')
            ->field('month.*,e.name as enginname')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_month', 'month'))
            ->order('month.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('speed_month');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_speed_month(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入月计划名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择计划月份');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            if((new SceneService())->check_speed_month_name($param['name'], $param['month'], $param['enginid'])){
                return outputJson(4000, '该月份内月计划名称已存在');
            }


            $param['number'] = (new SceneService())->create_speed_month_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }
            Db::startTrans();
            try {
                $id = SpeedMonthModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['monthid'] = $id;
                    SpeedMonthListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_month',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'speed_month', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'speed_month', $id, '月进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SceneService())->create_speed_month_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_speed_month(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SpeedMonthModel::where(['id'=>$param['id']])->find();


            if(empty($param['name'])){
                return outputJson(4000, '请输入月计划名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择计划月份');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            if((new SceneService())->check_speed_month_name($param['name'], $param['month'], $old['enginid'], $old['id'])){
                return outputJson(4000, '该月份内月计划名称已存在');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }

            Db::startTrans();
            try {
                SpeedMonthModel::update($param);

                SpeedMonthListModel::where(['monthid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['monthid'] = $param['id'];
                    SpeedMonthListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_month',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'speed_month', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'speed_month', $param['id'], '月进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SpeedMonthModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = SpeedMonthListModel::where(['monthid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('speed_month', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_speed_month(Request $request){
        $param = $request->post();
        $data = SpeedMonthModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            SpeedMonthModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'speed_month', $data['id'], '月进度计划');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_speed_month(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SpeedMonthModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = SpeedMonthListModel::where(['monthid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('speed_month', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_speed(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['speed.deleted_user','=', 0];
        $where[] = ['speed.comid','=',$this->userinfo['comid']];
        $where[] = ['speed.isturn','=',1];
        $where[] = ['speed.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|speed.number|speed.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['speed.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['speed.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['speed.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['speed.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['speed.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginSpeedModel::alias('speed')
            ->field('speed.*,e.name as enginname')
            ->join('engin e','e.id=speed.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_speed', 'speed'))
            ->order('speed.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_speed');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入填报名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择填报月份');
            }

            if((new SceneService())->check_engin_speed_month($param['month'], $param['enginid'])){
                return outputJson(4000, '该项目填报月份已存在');
            }

            if(empty($param['benjin'])){
                return outputJson(4000, '请输入本期完成进度');
            }
            if(empty($param['leijin'])){
                return outputJson(4000, '请输入累计完成进度');
            }

            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new SceneService())->create_engin_speed_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入填报明细');
            }

            Db::startTrans();
            try {
                $id = EnginSpeedModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['speedid'] = $id;
                    EnginSpeedListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'engin_speed',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_speed', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_speed', $id, '进度填报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SceneService())->create_engin_speed_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginSpeedModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入填报名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择填报月份');
            }
            if((new SceneService())->check_engin_speed_month($param['month'], $old['enginid'], $param['id'])){
                return outputJson(4000, '该项目填报月份已存在');
            }
            if(empty($param['benjin'])){
                return outputJson(4000, '请输入本期完成进度');
            }
            if(empty($param['leijin'])){
                return outputJson(4000, '请输入累计完成进度');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入填报明细');
            }

            Db::startTrans();
            try {
                EnginSpeedModel::update($param);

                EnginSpeedListModel::where(['speedid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['speedid'] = $param['id'];
                    EnginSpeedListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'engin_speed',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_speed', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_speed', $param['id'], '进度填报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginSpeedModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginSpeedListModel::where(['speedid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_speed', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_speed(Request $request){
        $param = $request->post();
        $data = EnginSpeedModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginSpeedModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_speed', $data['id'], '进度填报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_speed(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginSpeedModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['monthinfo'] = SpeedMonthModel::where(['id'=>$data['data']['monthid']])->find();


        $list = EnginSpeedListModel::where(['speedid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['zhanbi'] = 0;
            if($item['finnum'] != 0 && $item['shinum'] != 0){
                $item['zhanbi'] = round($item['shinum']/$item['finnum']*100, 2);
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('engin_speed', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_engin_chan(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['chan.deleted_user','=', 0];
        $where[] = ['chan.comid','=',$this->userinfo['comid']];
        $where[] = ['chan.isturn','=',1];
        $where[] = ['chan.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|chan.number|chan.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['chan.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['chan.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['chan.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['benjin']) && !empty($param['benjin'])){
            $where[] = ['chan.benjin','like', '%'.$param['benjin'].'%'];
        }
        if(isset($param['leijin']) && !empty($param['leijin'])){
            $where[] = ['chan.leijin','like', '%'.$param['leijin'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chan.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginChanModel::alias('chan')
            ->field('chan.*,e.name as enginname')
            ->join('engin e','e.id=chan.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_chan', 'chan'))
            ->order('chan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_chan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入填报名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择填报月份');
            }

            if((new SceneService())->check_engin_chan_month($param['month'], $param['enginid'])){
                return outputJson(4000, '该项目填报月份已存在');
            }

            if(empty($param['benjin'])){
                return outputJson(4000, '请输入本次填报产值');
            }
            if(empty($param['leijin'])){
                return outputJson(4000, '请输入累计完成产值');
            }

            $param['number'] = (new SceneService())->create_engin_chan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                $id = EnginChanModel::insertGetId($param);

                foreach($list as $item){
                    $item['chanid'] = $id;
                    EnginChanListModel::insert($item);
                }

                (new OptService())->relation_file($files,'engin_chan',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_chan', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_chan', $id, '产值填报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new SceneService())->create_engin_chan_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginChanModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入填报名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择填报月份');
            }
            if((new SceneService())->check_engin_chan_month($param['month'], $old['enginid'], $param['id'])){
                return outputJson(4000, '该项目填报月份已存在');
            }
            if(empty($param['benjin'])){
                return outputJson(4000, '请输入本次填报产值');
            }
            if(empty($param['leijin'])){
                return outputJson(4000, '请输入累计完成产值');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                EnginChanModel::update($param);


                (new OptService())->relation_file($files,'engin_chan',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_chan', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_chan', $param['id'], '产值填报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;



            $data['files'] = (new OptService())->get_file('engin_chan', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_chan(Request $request){
        $param = $request->post();
        $data = EnginChanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginChanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_chan', $data['id'], '产值填报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_chan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginChanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginChanListModel::where(['chanid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('engin_chan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_speed_jingen(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['monthname']) && !empty($param['monthname'])){
            $where[] = ['month.name','like', '%'.$param['monthname'].'%'];
        }
        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['list.code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['list.buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['list.xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        if(!isset($param['month']) || empty($param['month'])){
            $seamonth = date("Y-m", strtotime('-1 month')).' - '. date("Y-m", strtotime('-1 month'));
        }else{
            $seamonth = $param['month'];
        }


        $where[] = ['month.month','between', explode(' - ', $seamonth)];

        $data['is_month'] = $seamonth;


        $data['data'] = SpeedMonthListModel::alias('list')
            ->field('list.*,e.name as enginname,month.month,month.name as monthname')
            ->join('speed_month month','month.id=list.monthid')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_jingen', 'month'))
            ->order('month.enginid DESC, month.month DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['benshinum'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1,'list.monthid'=>$item['id']])
                    ->sum('shinum');
                $item['benshiday'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1,'list.monthid'=>$item['id']])
                    ->sum('shiday');
                $item['zhanbi'] = 0;
                $item['sname'] = false;
                if($item['finnum'] != 0){
                    $item['zhanbi'] = round($item['benshinum']/$item['finnum']*100,2);
                }
                if($item['zhanbi'] >= 100){
                    $item['sname'] = true;
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_speed_jinhui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zong.deleted_user','=', 0];
        $where[] = ['zong.comid','=',$this->userinfo['comid']];



        $enginid = 0;
        if(!isset($param['enginid']) || empty($param['enginid'])){
            $enginid = EnginSpeedModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'comid'=>$this->userinfo['id']])
                ->order('month DESC')->value('enginid');
        }else{
            $enginid = $param['enginid'];
        }
        $where[] = ['zong.enginid','=',$enginid];

        $data['enginidArr'] = EnginModel::field('id as value,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])->select()->toArray();
        $data['is_enginid'] = $enginid;


        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['list.code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['list.buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['list.xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }




        $data['data'] = SpeedZongListModel::alias('list')
            ->field('list.*,e.name as enginname,zong.enginid')
            ->join('speed_zong zong','zong.id=list.zongid')
            ->join('engin e','e.id=zong.enginid')
            ->where((new MenuService())->get_miss_where('speed_jinhui', 'zong'))
            ->where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shifindt'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->value('findt');

                $item['leishinum'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->sum('shinum');

                $item['leishichan'] = round($item['leishinum']*$item['price'], 2);
                $item['leishiday'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->sum('shiday');

                $item['dangjin'] = 0;
                if($item['num'] != 0){
                    $item['dangjin'] = round($item['leishinum']/$item['num']*100, 2);
                }

                $item['gongbi'] = 0;
                if($item['duration'] != 0){
                    $item['gongbi'] = round($item['leishiday']/$item['duration']*100, 2);
                }

                $item['gongjing'] = false;
                if($item['dangjin'] >= $item['gongbi']){
                    $item['gongjing'] = true;
                }

                $item['dainum'] = round($item['num']-$item['leishinum'], 3);
                $item['daichan'] = round($item['chanfee']-$item['leishichan'], 2);
                $item['sheng'] = $item['duration']-$item['leishiday'];

                $item['zuijin'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->order('speed.month DESC')
                    ->value('speed.month');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_speed_yuechan(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $year = $param['year'];

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_yuechan', 'e'))
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($year){
                $item['year'] = $year;
                $zong = SpeedZongModel::where(['enginid'=>$item['id'],'deleted_user'=>0])->find();
                $item['zongid'] = isset($zong['id']) ? $zong['id'] : 0;
                $item['chanfee'] = isset($zong['chanfee']) ? $zong['chanfee'] : 0;

                $item['wanchan'] = 0;
                $item['wanbi'] = 0;
                $item['leichan'] = 0;

                for ($i=1; $i<=12; $i++){
                    $item['wan'.$i] = 0;
                    $item['shen'.$i] = 0;
                }
                $item['wanyear'] = 0;
                $item['shenyear'] = 0;


                $chanList = EnginChanModel::field('month,leijin')
                    ->where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id']])
                    ->select()->toArray();
                foreach ($chanList as $vv){
                    for ($i=1; $i<=12; $i++){
                        $days = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

                        if($vv['month'] == $days){
                            $item['shen'.$i] += $vv['leijin'];
                            $item['shenyear'] += $vv['leijin'];
                        }
                    }
                    $item['leichan'] += $vv['leijin'];
                }


                $zongList = SpeedZongListModel::where(['zongid'=>$zong['id']])->select()->toArray();
                foreach ($zongList as $v){
                    $baoList = EnginSpeedListModel::alias('list')
                        ->field('speed.month,list.shinum')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['id'],'list.zongid'=>$v['id']])
                        ->select()->toArray();
                    foreach ($baoList as $vv){
                        $value = $vv['shinum'] * $v['price'];
                        for ($i=1; $i<=12; $i++){
                            $days = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;
                            if($vv['month'] == $days){
                                $item['wan'.$i] += $value;
                                $item['wanyear'] += $value;
                            }
                        }
                        $item['wanchan'] += $value;
                    }



                }

                if($item['chanfee'] != 0){
                    $item['wanbi'] = round($item['wanchan'] / $item['chanfee'] *100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }













    public function list_engin_xun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['xun.deleted_user','=', 0];
        $where[] = ['xun.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|xun.number|xun.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xun.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['xun.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xuntype']) && !empty($param['xuntype'])){
            $where[] = ['xun.xuntype','=', $param['xuntype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['xun.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $wherexundept = '';
        if(isset($param['xundept']) && !empty($param['xundept'])){
            $wherexundept = Db::raw("find_in_set(".$param['xundept'].",xundept)");
        }

        $wherexuner = '';
        if(isset($param['xuner']) && !empty($param['xuner'])){
            $wherexuner = Db::raw("find_in_set(".$param['xuner'].",xuner)");
        }

        $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
        $data['is_xuntype'] = isset($param['xuntype']) ? $param['xuntype'] : '';

        $data['data'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->order('xun.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $item['xuntype']);
                $item['xundept_name'] = DeptModel::get_name_by_ids($item['xundept']);
                $item['xuner_name'] = UserModel::get_realname_by_ids($item['xuner']);


                $zheng_number = EnginZhengModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'xunid'=>$item['id']])
                    ->column('number');
                $item['zheng_number'] = implode(',',$zheng_number);
                $item['zhengnum'] = count($zheng_number);
                return $item;
            })->toArray();


        $data['all'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->count();
        $data['toyear'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where('xun.dt', 'egt', date("Y-01-01"))
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->count();

        $data['butauth'] = (new MenuService())->get_butauth('engin_xun');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_xun(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入巡检名称');
            }
            if(empty($param['xuntype'])){
                return outputJson(4000, '请选择巡检类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择巡检日期');
            }

            $param['number'] = (new SceneService())->create_engin_xun_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $istong = $param['istong'];
            unset($param['istong']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入存在问题');
            }

            Db::startTrans();
            try {
                $id = EnginXunModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['xunid'] = $id;
                    EnginXunListModel::insert($vv);
                }

                if($istong > 0){
                    $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                    $xuntype = (new OptService())->get_base_showname('scene_xuntype', $param['xuntype']);
                    $mess = "巡检报告-".$enginname."-".$xuntype."-".$param['number'];
                    (new OptService())->add_todo($param['tonger'], '巡检报告',$mess, $id, 'engin_xun');
                }


                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_xun', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_xun', $id, '质安巡检报告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['number'] = (new SceneService())->create_engin_xun_number();
            $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_xun(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginXunModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入巡检名称');
            }
            if(empty($param['xuntype'])){
                return outputJson(4000, '请选择巡检类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择巡检日期');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $istong = $param['istong'];
            unset($param['istong']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入存在问题');
            }

            Db::startTrans();
            try {
                EnginXunModel::update($param);

                EnginXunListModel::where(['xunid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['xunid'] = $param['id'];
                    EnginXunListModel::insert($vv);
                }

                if($istong > 0){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $xuntype = (new OptService())->get_base_showname('scene_xuntype', $param['xuntype']);
                    $mess = "巡检报告-".$enginname."-".$xuntype."-".$old['number'];
                    (new OptService())->add_todo($param['tonger'], '巡检报告',$mess, $param['id'], 'engin_xun');
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_xun', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_xun', $param['id'], '质安巡检报告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginXunModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['xundept_info'] = (new UserService())->get_users_part_arr($data['data']['xundept'], 'dept');
            $data['data']['xuner_info'] = (new UserService())->get_users_part_arr($data['data']['xuner'], 'user');
            $data['data']['tonger_info'] = (new UserService())->get_users_part_arr($data['data']['tonger'], 'user');


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
            $data['level'] = (new SceneService())->get_engin_xun_level();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_xun(Request $request){
        $param = $request->post();
        $data = EnginXunModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginXunModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_xun', $data['id'], '质安巡检报告');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_xun(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginXunModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $data['data']['xuntype']);
        $data['data']['xundept_name'] = DeptModel::get_name_by_ids($data['data']['xundept']);
        $data['data']['xuner_name'] = UserModel::get_realname_by_ids($data['data']['xuner']);
        $data['data']['tonger_name'] = UserModel::get_realname_by_ids($data['data']['tonger']);


        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['cusname'] = count($item['filearr']);
        }
        $data['list'] = $list;
        $data['files'] = (new OptService())->get_file('engin_xun', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_engin_zheng(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zheng.deleted_user','=', 0];
        $where[] = ['zheng.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|xun.number|zheng.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['xunid']) && !empty($param['xunid'])){
            $where[] = ['zheng.xunid','=', $param['xunid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zheng.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['xunnumber']) && !empty($param['xunnumber'])){
            $where[] = ['xun.number','like', '%'.$param['xunnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['zheng.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['zheng.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->order('zheng.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['fuzeer_name'] = UserModel::get_realname_by_ids($item['fuzeer']);
                $item['gaista_name'] = (new SceneService())->get_zheng_gaista_name($item['gaista']);
                $item['fusta_name'] = (new SceneService())->get_zheng_fusta_name($item['fusta']);

                $item['is_fuzeer'] = in_array($this->userinfo['id'], explode(',', $item['fuzeer'])) ? true : false;
                $item['is_fujianer'] = $this->userinfo['id'] == $item['fujianer'] ? true : false;
                return $item;
            })->toArray();



        $data['all'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->count();

        $data['zhengn_um'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.gaista', '=', 1)
            ->count();

        $data['zhengnum_no'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.gaista', '=', 0)
            ->count();

        $data['zhengnum_yu'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.dt', '<', date("Y-m-d"))
            ->where('zheng.gaista', '=', 0)
            ->count();


        $data['butauth'] = (new MenuService())->get_butauth('engin_zheng');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['number'] = (new SceneService())->create_engin_zheng_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                $id = EnginZhengModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhengid'] = $id;
                    EnginZhengListModel::insert($vv);
                }


                $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                $fuzeerName = UserModel::get_realname_by_ids($param['fuzeer']);
                $mess = "整改通知-".$enginname."-".$param['number']."-".$fuzeerName;
                (new OptService())->add_todo($param['fuzeer'], '整改通知',$mess, $id, 'engin_zheng');


                (new OptService())->relation_file($files,'engin_zheng',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_zheng', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_zheng', $id, '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new SceneService())->create_engin_zheng_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function wai_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['number'] = (new SceneService())->create_engin_zheng_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                $id = EnginZhengModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhengid'] = $id;
                    EnginZhengListModel::insert($vv);
                }

                $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                $fuzeerName = UserModel::get_realname_by_ids($param['fuzeer']);
                $mess = "整改通知-".$enginname."-".$param['number']."-".$fuzeerName;
                (new OptService())->add_todo($param['fuzeer'], '整改通知',$mess, $id, 'engin_zheng');

                (new OptService())->relation_file($files,'engin_zheng',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_zheng', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_zheng', $id, '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginXunModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['xunidinfo'] = $data['data'];



            $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])
                ->where(['iszheng'=>'是'])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['number'] = (new SceneService())->create_engin_zheng_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();

            if($old['fusta'] > 0){
                return outputJson(4000, '已经整改过，不能编辑');
            }

            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                EnginZhengListModel::where(['zhengid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhengid'] = $param['id'];
                    EnginZhengListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_zheng', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $xunidinfo = EnginXunModel::get_info_by_pk($data['data']['xunid']);
            $data['xunidinfo'] = $xunidinfo;

            $data['data']['fuzeer_info'] = (new UserService())->get_users_part_arr($data['data']['fuzeer'], 'user');
            $data['data']['fujianer_info'] = (new UserService())->get_users_part_arr($data['data']['fujianer'], 'user');

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_zheng(Request $request){
        $param = $request->post();
        $data = EnginZhengModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginZhengModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_zheng', $data['id'], '整改通知');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_zheng(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginZhengModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['deptid_name'] = DeptModel::get_name_by_pk($data['data']['deptid']);
        $data['data']['gaista_name'] = (new SceneService())->get_zheng_gaista_name($data['data']['gaista']);
        $data['data']['fusta_name'] = (new SceneService())->get_zheng_fusta_name($data['data']['fusta']);



        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['xunidinfo'] = EnginXunModel::get_info_by_pk($data['data']['xunid']);

        $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);
        $data['data']['fujianer_name'] = UserModel::get_realname_by_pk($data['data']['fujianer']);

        $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['cusname'] = count($item['filearr']);

            $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
            $item['gaicusname'] = count($item['gaifilearr']);


            $item['files'] = $item['filearr'];
            $item['filesnum'] = count($item['files']);

            $item['gaifiles'] = $item['gaifilearr'];
            $item['gaifilesnum'] = count($item['gaifiles']);
        }
        $data['list'] = $list;


        $data['data']['is_fuzeer'] = in_array($this->userinfo['id'], explode(',', $data['data']['fuzeer'])) ? true : false;
        $data['data']['is_fujianer'] = $this->userinfo['id'] == $data['data']['fujianer'] ? true : false;
        $data['butauth'] = (new MenuService())->get_butauth('engin_zheng');
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gai_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();


            if(empty($param['gaidt'])){
                return outputJson(4000, '请选择整改完成时间');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入整改回复信息');
            }

            $param['gaista'] = 1;
            $param['fusta'] = 1;
            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                foreach($list as $vv){
                    EnginZhengListModel::update($vv);
                }

                $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                $mess = "整改回复-".$enginname."-".$old['number'];
                (new OptService())->add_todo($old['fujianer'], '整改回复',$mess, $old['id'], 'engin_zheng');


                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new OptService())->add_flow_log('整改回复', 'engin_zheng', $param['id']);
                (new OptService())->add_log_list('整改回复', 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);

            $gaiWhere = [];
            if($data['data']['gaista'] == 0 && $data['data']['fusta'] == 3){
                $gaiWhere[] = ['fuguo', '=', '不通过'];
            }

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->where($gaiWhere)->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = $item['filearr'];
                $item['filesnum'] = count($item['files']);

                $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
                $item['gaifiles'] = $item['gaifilearr'];
                $item['gaifilesnum'] = count($item['gaifiles']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fu_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();


            if(empty($param['fudt'])){
                return outputJson(4000, '请选择复检时间');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入整改回复信息');
            }




            $allfuguo = true;
            foreach ($list as $v){
                if($v['fuguo'] == '不通过'){
                    $allfuguo = false;
                }
            }

            if($allfuguo){
                $param['gaista'] = 1;
                $param['fusta'] = 2;
            }else{
                $param['gaista'] = 0;
                $param['fusta'] = 3;
            }

            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                foreach($list as $vv){
                    EnginZhengListModel::update($vv);
                }

                if($param['fusta'] == 2){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $mess = "复检验收-".$enginname."-".$old['number']. ' - 复检通过';
                    (new OptService())->add_todo($old['fuzeer'], '复检验收',$mess, $old['id'], 'engin_zheng');
                }
                if($param['fusta'] == 3){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $mess = "复检验收-".$enginname."-".$old['number']. ' - 复检未通过，部分检查项需重新整改';;
                    (new OptService())->add_todo($old['fuzeer'], '复检验收',$mess, $old['id'], 'engin_zheng');
                }

                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new OptService())->add_flow_log('复检验收', 'engin_zheng', $param['id']);
                (new OptService())->add_log_list('复检验收', 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = $item['filearr'];
                $item['filesnum'] = count($item['files']);

                $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
                $item['gaifiles'] = $item['gaifilearr'];
                $item['gaifilesnum'] = count($item['gaifiles']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function list_engin_jian(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jian.deleted_user','=', 0];
        $where[] = ['jian.comid','=',$this->userinfo['comid']];



        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['jian.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['jiantype']) && !empty($param['jiantype'])){
            $where[] = ['jian.jiantype','=', $param['jiantype']];
        }


        $data['data'] = EnginJianModel::alias('jian')
            ->field('jian.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_jian', 'jian'))
            ->order('jian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $item['jiantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_jian');
        $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_jian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jiantype'])){
                return outputJson(4000, '请选择检查项类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入检查项');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginJianModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_jian',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_jian', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_jian', $id, '检查项维护');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_jian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginJianModel::where(['id'=>$param['id']])->find();

            if(empty($param['jiantype'])){
                return outputJson(4000, '请选择检查项类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入检查项');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginJianModel::update($param);
                (new OptService())->relation_file($files,'engin_jian',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_jian', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_jian', $param['id'], '检查项维护');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginJianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
            $data['files'] = (new OptService())->get_file('engin_jian', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_jian(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要删除的数据');
        }

        $data = EnginJianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            foreach ($ids as $id){
                $data = EnginJianModel::where(['id'=>$id,'comid'=>$this->userinfo['comid']])->find();
                if(!$data){
                    continue;
                }
                EnginJianModel::where(['id' => $data['id']])->update([
                    'deleted_user' => $this->userinfo['id'],
                    'deleted_at' => date('Y-m-d H:i:s')
                ]);
                (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_jian', $data['id'], '检查项维护');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_jian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginJianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $data['data']['jiantype']);
        $data['files'] = (new OptService())->get_file('engin_jian', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }








    public function list_engin_engin_fold(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_fold', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);

                $item['filenums'] = EnginFoldModel::where(['enginid'=>$item['id'],'type'=>1])->count();

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_fold');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_fold(Request $request){
        $param = $request->post();
        get_page($param);




        $where[] = ['fold.comid','=',$this->userinfo['comid']];
        $where[] = ['fold.pid','=',$param['pid']];
        $where[] = ['fold.enginid','=',$param['enginid']];

        if(isset($param['wenname']) && !empty($param['wenname'])){
            $where[] = ['fold.name','like', '%'.$param['wenname'].'%'];
        }

        $data['data'] = EnginFoldModel::alias('fold')
            ->field('fold.*,u.realname as created_user_name')
            ->leftJoin('user u','fold.created_user=u.id')
            ->where($where)
            ->order('fold.type ASC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['sonnums'] = (new SceneService())->get_fold_filenums($item['id']);
                return $item;
            })->toArray();

        $data['curpid'] = $param['pid'];
        $data['shangpid'] = 0;
        if($data['curpid'] > 0){
            $data['shangpid'] = EnginFoldModel::where(['id'=>$data['curpid']])->value('pid');
        }

        $data['engininfo'] = EnginModel::get_info_by_pk($param['enginid']);
        $data['butauth'] = (new MenuService())->get_butauth('engin_fold');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function addjia_engin_fold(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入名称');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');
            $param['type'] = 0;
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginFoldModel::insertGetId($param);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_fold', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_fold', $id, '项目资料库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function editjia_engin_fold(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入名称');
            }
            unset($param['token']);

            Db::startTrans();
            try {
                EnginFoldModel::update($param);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_fold', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_fold', $param['id'], '项目资料库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data = EnginFoldModel::where(['id'=>$id])->find();
            if(!$data){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_fold(Request $request){
        $param = $request->post();
        $data = EnginFoldModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        if($data['type'] == 0){
            $exist = EnginFoldModel::where(['pid'=>$data['id']])->find();
            if($exist){
                return outputJson(4000, '该文件夹下存在子级数据，不能删除');
            }
        }

        Db::startTrans();
        try {
            EnginFoldModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_fold', $data['id'], '项目资料库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function move_engin_fold(Request $request){
        if($request->isPost()){
            $param = $request->post();

            unset($param['token']);

            Db::startTrans();
            try {
                EnginFoldModel::update($param);
                (new OptService())->add_flow_log('移动', 'engin_fold', $param['id']);
                (new OptService())->add_log_list('移动', 'engin_fold', $param['id'], '项目资料库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data = EnginFoldModel::where(['id'=>$id])->find();
            if(!$data){
                return outputJson(4000, lang('code_4000'));
            }

            $data['foldArr'] = (new SceneService())->get_engin_fold_all(0, $data['enginid']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function list_engin_stamp(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['stamp.deleted_user','=', 0];
        $where[] = ['stamp.comid','=',$this->userinfo['comid']];
        $where[] = ['stamp.isturn','=',1];
        $where[] = ['stamp.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['gai.flowuser|gai.flowdept|list.name|gaier|list.jieer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['stamp.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['zitype']) && !empty($param['zitype'])){
            $where[] = ['stamp.zitype','=', $param['zitype']];
        }
        if(isset($param['isgai']) && $param['isgai'] != ''){
            $where[] = ['stamp.isgai','=', $param['isgai']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['stamp.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['stamp.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['stamp.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
        $data['is_zitype'] = isset($param['zitype']) ? $param['zitype'] : '';

        $data['data'] = EnginStampModel::alias('stamp')
            ->field('stamp.*,e.name as enginname')
            ->join('engin e','e.id=stamp.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_stamp', 'stamp'))
            ->order('stamp.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zitype'] = (new OptService())->get_base_showname('scene_zitype', $item['zitype']);
                $item['isgai_name'] = $item['isgai'] > 0 ? '已盖章' : '未盖章';
                $item['nums'] = EnginStampListModel::where(['stampid'=>$item['id']])->sum('num');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_stamp');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['zitype'])){
                return outputJson(4000, '请选择资料类型');
            }

            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['status'] = 1;
            $newparam['isturn'] = 1;
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();


            $list = array_filter($param['list']);
            unset($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            $newparam['enginid'] = $param['enginid'];
            $newparam['zitype'] = $param['zitype'];


            Db::startTrans();
            try {
                $id = EnginStampModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['stampid'] = $id;
                    EnginStampListModel::insert($vv);
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_stamp', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_stamp', $id, '项目资料盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['shener'] = $this->userinfo['realname'];
            $data['shendept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginStampModel::where(['id'=>$param['id']])->find();

            if(empty($param['zitype'])){
                return outputJson(4000, '请选择资料类型');
            }
            if(empty($param['shener'])){
                return outputJson(4000, '请输入申请人');
            }
            if(empty($param['shendept'])){
                return outputJson(4000, '请输入申请部门');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }


            $newparam['id'] = $param['id'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['shener'] = $param['shener'];
            $newparam['shendept'] = $param['shendept'];

            Db::startTrans();
            try {
                EnginStampModel::update($newparam);

                EnginStampListModel::where(['stampid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['stampid'] = $param['id'];
                    EnginStampListModel::insert($vv);
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id'], '项目资料盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['yongtype'] = explode(',', $item['yongtype']);
            }
            $data['list'] = $list;

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_stamp(Request $request){
        $param = $request->post();
        $data = EnginStampModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginStampModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_stamp', $data['id'], '项目资料盖章');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_stamp(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginStampModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['zitype'] = (new OptService())->get_base_showname('scene_zitype', $data['data']['zitype']);


        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
            $item['cusname'] = count($item['filearr']);
        }
        $data['list'] = $list;

        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gai_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginStampModel::where(['id'=>$param['id']])->find();


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                EnginStampModel::where(['id'=>$param['id']])->update(['isgai'=>1]);
                foreach($list as $vv){
                    EnginStampListModel::update($vv);
                }

                (new OptService())->add_flow_log('盖章', 'engin_stamp', $param['id']);
                (new OptService())->add_log_list('盖章', 'engin_stamp', $param['id'], '项目资料盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                if($data['data']['isgai'] < 1){
                    $item['gaidt'] = date("Y-m-d");
                    $item['gaier'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function list_engin_stamp_detail(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['stamp.deleted_user','=', 0];
        $where[] = ['stamp.comid','=',$this->userinfo['comid']];
        $where[] = ['stamp.isturn','=',1];
        $where[] = ['stamp.isgai','=',1];
        $where[] = ['stamp.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['stamp.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['zitype']) && !empty($param['zitype'])){
            $where[] = ['stamp.zitype','=', $param['zitype']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['stamp.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['stamp.flowdept','like', '%'.$param['flowdept'].'%'];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }

        $whereYongtype = '';
        if(isset($param['yongtype']) && !empty($param['yongtype'])){
            $whereYongtype = Db::raw("find_in_set(".$param['yongtype'].",yongtype)");
        }

        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['list.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['isfan']) && !empty($param['isfan'])){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['gaidt']) && !empty($param['gaidt'])){
            $where[] = ['list.gaidt','between', explode(' - ', $param['gaidt'])];
        }
        if(isset($param['gaier']) && !empty($param['gaier'])){
            $where[] = ['list.gaier','=', $param['gaier']];
        }
        if(isset($param['fandt']) && !empty($param['fandt'])){
            $where[] = ['list.fandt','between', explode(' - ', $param['fandt'])];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['list.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
        $data['is_zitype'] = isset($param['zitype']) ? $param['zitype'] : '';

        $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
        $data['is_yongtype'] = isset($param['yongtype']) ? $param['yongtype'] : '';

        $data['data'] = EnginStampListModel::alias('list')
            ->field('list.*,e.name as enginname,stamp.zitype,stamp.flowuser,stamp.flowdept,stamp.created_at')
            ->join('engin_stamp stamp','stamp.id=list.stampid')
            ->join('engin e','e.id=stamp.enginid')
            ->where($where)
            ->where($whereYongtype)
            ->where((new MenuService())->get_miss_where('engin_stamp_detail', 'stamp'))
            ->order('stamp.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zitype'] = (new OptService())->get_base_showname('scene_zitype', $item['zitype']);
                $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_stamp_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function fan_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                foreach($list as $vv){
                    EnginStampListModel::update($vv);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ids = $request->get('ids',0,'string');

            $list = EnginStampListModel::where('id','in',$ids)->where(['isfan'=>'是'])->select()->toArray();
            foreach ($list as &$item){
                $stamp = EnginStampModel::where(['id'=>$item['stampid']])->find();
                $item['shener'] = $stamp['flowuser'];
                $item['enginname'] = EnginModel::where(['id'=>$stamp['enginid']])->value('name');

                if(empty($item['fandt'])){
                    $item['fandt'] = date("Y-m-d");
                }
                if(empty($item['jieer'])){
                    $item['jieer'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function list_visa(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['visa.deleted_user','=', 0];
        $where[] = ['visa.comid','=',$this->userinfo['comid']];
        $where[] = ['visa.isturn','=',1];
        $where[] = ['visa.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|visa.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['visa.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['visa.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['visatype']) && !empty($param['visatype'])){
            $where[] = ['visa.visatype','=', $param['visatype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['visa.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['visa.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
        $data['is_visatype'] = isset($param['visatype']) ? $param['visatype'] : '';

        $data['data'] = VisaModel::alias('visa')
            ->field('visa.*,e.name as enginname')
            ->join('engin e','e.id=visa.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('visa', 'visa'))
            ->order('visa.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['visatype'] = (new OptService())->get_base_showname('scene_visatype', $item['visatype']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('visa');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_visa(Request $request){
        $param = $request->post();
        $data = VisaModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            VisaModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'visa', $data['id'], '签证单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_visa(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = VisaModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['visatype'] = (new OptService())->get_base_showname('scene_visatype', $data['data']['visatype']);

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['files'] = (new OptService())->get_file('visa', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }





}
